<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评分" prop="rating">
        <el-input v-model="queryParams.rating" placeholder="请输入评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['service:review:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="服务详情名称" align="center" prop="serviceOptionName" />
      <el-table-column label="用户昵称" align="center" prop="userNickName">
        <template slot-scope="scope">
          {{ anonymizeNickname(scope.row.userNickName) }}
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="rating">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.rating" disabled show-score text-color="#ff9900" score-template="{value}">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="comment" />
      <el-table-column label="是否包含图片" align="center">
        <template slot-scope="scope">
          {{ scope.row.hasImages === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row.id)"
            v-hasPermi="['service:review:view']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 用户评价详情对话框 -->
    <el-dialog :title="detailTitle" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-form ref="detailForm" :model="detailForm" label-width="120px">
        <el-form-item label="编号">
          {{ detailForm.id }}
        </el-form-item>
        <el-form-item label="服务详情名称">
          {{ detailForm.serviceOptionName }}
        </el-form-item>
        <el-form-item label="用户昵称">
          {{ anonymizeNickname(detailForm.userNickName) }}
        </el-form-item>
        <el-form-item label="评分">
          <el-rate v-model="detailForm.rating" disabled show-score text-color="#ff9900" score-template="{value}">
          </el-rate>
        </el-form-item>
        <el-form-item label="评论内容">
          {{ detailForm.comment }}
        </el-form-item>
        <el-form-item label="是否包含图片">
          {{ detailForm.hasImages ? '是' : '否' }}
        </el-form-item>
        <el-form-item label="创建时间">
          {{ detailForm.createTime }}
        </el-form-item>
        <el-form-item label="评价图片">
          <div v-for="(image, index) in detailForm.reviewImages" :key="index">
            <img :src="image.imageUrl" alt="评价图片" style="max-width: 100%; max-height: 100%;">
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReview, getReview, delReview } from "@/api/feedback/review";

export default {
  name: "Review",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户评价表格数据
      reviewList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rating: null,
        comment: null,
        hasImages: null,
      },
      detailOpen: false,
      detailTitle: "用户评价详情",
      detailForm: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查询评价详情
    handleDetail(id) {
      getReview(id).then(response => {
        this.detailForm = response.data
        this.detailOpen = true;
      });

    },
    //昵称匿名化处理
    anonymizeNickname(nickname) {
      if (!nickname) return '';
      const length = nickname.length;
      if (length <= 2) {
        return nickname[0] + '*';
      } else {
        return nickname.slice(0, 1) + '*'.repeat(length - 2) + nickname.slice(-1);
      }
    },
    /** 查询用户评价列表 */
    getList() {
      this.loading = true;
      listReview(this.queryParams).then(response => {
        this.reviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户评价编号为"' + ids + '"的数据项？').then(function () {
        return delReview(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
